home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / TWEAKFLC.ZIP / TIMER.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-01-15  |  1.8 KB  |  97 lines

  1. Unit Timer;
  2.  
  3. Interface
  4.  
  5. Uses Crt, Dos;
  6.  
  7. procedure StartTimer;
  8. procedure WaitFor(tick: LongInt);
  9. procedure InstallFastTimer;
  10. procedure RestoreTimer;
  11.  
  12. Implementation
  13.  
  14. {$F+} { Force far mode, a good idea when mucking around with interrupts }
  15.  
  16. const TIMERINTR = 8;
  17.       PIT_FREQ = $1234DD;
  18.  
  19. var BIOSTimerHandler : procedure;
  20.     clock_ticks, counter : longint;
  21.     curtick : longint;
  22.  
  23. procedure SetTimer(TimerHandler : pointer; frequency : word);
  24. begin
  25.  
  26.   { Do some initialization }
  27.   clock_ticks := 0;
  28.   counter := $1234DD div frequency;
  29.  
  30.   { Store the current BIOS handler and set up our own }
  31.   GetIntVec(TIMERINTR, @BIOSTimerHandler);
  32.   SetIntVec(TIMERINTR, TimerHandler);
  33.  
  34.   { Set the PIT channel 0 frequency }
  35.   Port[$43] := $34;
  36.   Port[$40] := counter mod 256;
  37.   Port[$40] := counter div 256;
  38. end;
  39.  
  40. procedure CleanUpTimer;
  41. begin
  42.   { Restore the normal clock frequency }
  43.   Port[$43] := $34;
  44.   Port[$40] := 0;
  45.   Port[$40] := 0;
  46.  
  47.   { Restore the normal ticker handler }
  48.   SetIntVec(TIMERINTR, @BIOSTimerHandler);
  49. end;
  50.  
  51. procedure Handler; Interrupt;
  52. begin
  53.  
  54.   { DO WHATEVER WE WANT TO DO IN HERE }
  55.   Inc(curtick);
  56.  
  57.   { Adjust the count of clock ticks }
  58.   clock_ticks := clock_ticks + counter;
  59.  
  60.   { Is it time for the BIOS handler to do it's thang? }
  61.   if clock_ticks >= $10000 then
  62.     begin
  63.  
  64.       { Yep! So adjust the count and call the BIOS handler }
  65.       clock_ticks := clock_ticks - $10000;
  66.  
  67.       asm pushf end;
  68.       BIOSTimerHandler;
  69.     end
  70.  
  71.   { If not then just acknowledge the interrupt }
  72.   else
  73.     Port[$20] := $20;
  74. end;
  75.  
  76. procedure InstallFastTimer;
  77. begin
  78.   SetTimer(Addr(Handler), 1000);
  79. end;
  80.  
  81. procedure RestoreTimer;
  82. begin
  83.  CleanUpTimer;
  84. end;
  85.  
  86. procedure StartTimer;
  87. begin
  88.  curtick:=0;
  89. end;
  90.  
  91. procedure WaitFor(tick: LongInt);
  92. begin
  93.  Repeat Until tick<=curtick;
  94. end;
  95.  
  96. end.
  97.